<template>
	<view>
		<view class="divattachlist">
			<view v-for="(f, i) of fileList" :key="i"
				:class="[!!f.mime && f.mime.indexOf('image') > -1 ? 'isimgdiv' : 'notimgdiv']">
				<view class="img-box" v-if="!!f.mime && f.mime.indexOf('image') > -1"  style="position: relative;">
					<view class="img-item">
						<image class="img" @click="FileView(f)" :src="f.url" mode="aspectFill">
						</image>
					</view>
				</view>
				<view v-else>
					<a @click="FileView(f)" style="display: flex; align-items: center;">
						<image :src="gettypeimg(f.mime)" style="width:2rem;height:2rem;margin:0 10px 0 0" />
						<view style="flex:1">{{f.name }}</view>
					</a>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		GetFilesSortFormat,
		GetFileTypeImg
	} from "@/common/util-file.js";
	export default {
		props: ["files"],
		data() {
			return {
				fileList: [], //文件信息
			}
		},
		// onShow() {
		// 		this.initAttachment();
		// },
		mounted() {
			this.initAttachment();
		},

		methods: {
			// 文档附件处理
			initAttachment() {
				let _self = this;
				_self.fileList = [];
				if ((!!_self.files)) {
					_self.fileList = GetFilesSortFormat(_self.files);
				}
			},

			gettypeimg(val) {
				return GetFileTypeImg(val);
			},

			FileView(file) {
				uni.downloadFile({
					url: file.url,
					success: function(res) {
						if (res.tempFilePath.indexOf(".png" || ".jpg") > -1) {
							var filePath = []
							filePath[0] = res.tempFilePath;
							// 预览图片 
							uni.previewImage({
								urls: filePath,
								success: function(res) {
									console.log('打开成功')
								},
								fail: function(res) {
									console.log(res)
								}
							})
						} else {
							var filePath = res.tempFilePath;
							uni.openDocument({
								filePath: filePath,
								success: function(res) {
									console.log('打开文档成功');
								},
								fail: function(res) {
									console.log(res)
								},
								complete: function(res) {
									console.log(res)
								}
							});
						}


					},
					fail: function(res) {
						console.log(res)
					}
				});
			}
		},
		watch: {
			files: {
				handler(item, index) { //监听到数据变化执行方法 
					this.initAttachment()
				},
				deep: true // 深度监听父组件传过来对象变化 
			}
		},
	}
</script>


<style lang='scss' scoped>
	.main-box {
		/* flex: 1; */
		overflow: scroll;
		background: #e9e9e9;
		height: 100%;
		padding-top: 10rpx;

		.main-box-item {
			display: flex;
			align-items: center;
			height: 50px;
			background: #FFFFFF;
			/* box-shadow: 0px 2px 5px #F0F0F0; */
			/* border-radius: 4px; */
			vertical-align: middle;
			padding: 0px 10px;
			border-bottom-color: #E6E6E6;
			border-bottom-style: solid;
			border-bottom-width: 0.5px;

			.box-info-label {
				margin: 0 10px;
				font-size: 14px;
				font-weight: 500;
			}

			.box-info-value {
				margin-left: 10px;
				font-size: 14px;
				color: #B5B5B5;
				flex: 1;
				display: flex;

				.img {
					height: 100rpx;
					width: 100rpx;
					margin-left: 20rpx;
				}
			}
		}

		.main-box-item2 {
			display: flex;
			flex-direction: column;
			/* align-items: center; */
			/* height: 50px; */
			background: #FFFFFF;
			/* box-shadow: 0px 2px 5px #F0F0F0; */
			/* border-radius: 4px; */
			vertical-align: middle;
			padding: 0px 10px;
			border-bottom-color: #E6E6E6;
			border-bottom-style: solid;
			border-bottom-width: 0.5px;

			.box-info-label {
				margin: 0 10px;
				font-size: 14px;
				font-weight: 500;
			}

			.box-info-value {
				margin-left: 10px;
				font-size: 14px;
				color: #B5B5B5;
				flex: 1;


			}
		}


		
	}
	.divattachlist {
		padding-left: 25px;
		margin: 10px 0;
	}
	
	.img-box {
		display: flex;
		width: 100%;
		padding: 0 40rpx;
		padding-left: 10rpx;
		flex-direction: row;
		flex-wrap: wrap;
	
		.img-item {
			/* width:210rpx;
		height:210rpx; */
			width: 150rpx;
			height: 150rpx;
			border-radius: 16rpx;
			position: relative;
			/* padding: 9rpx 0; */
			margin: 5rpx 10rpx;
	
			.img {
				width: 150rpx;
				height: 150rpx;
				border-radius: 16rpx;
			}
		}
	}
	.divattachlist .isimgdiv {
		width: 150rpx;
		height: 150rpx;
		display: inline-block;
		text-align: center;
		margin-right:10px;
	}
	
/* 	.divattachlist .isimgdiv img {
		height: 4.9rem;
		width: 90%;
		margin: 0 11%;
	} */
	
/* 	.divattachlist .isimgdiv .imgdel1 {
		width: 1rem;
		height: 1rem;
		position: absolute;
		right: 0;
		margin: 0 5%;
	}
	 */
/* 	.divattachlist .isimgdiv .img1 {
		height: 5rem;
		width: 90%;
		margin: 0 4%;
	}
	 */
	.divattachlist .notimgdiv {
		padding: 5px 5px 0 5px;
		font-size: 15px;
	}
	
	
</style>
